package com.slh.touchandroid.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.IntentCompat;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.webkit.MimeTypeMap;
import android.widget.EditText;

public class FileUtils {
	public static String getMimeType(String path) {
		MimeTypeMap map = MimeTypeMap.getSingleton();
		String ext;
		try {
			ext = MimeTypeMap.getFileExtensionFromUrl(URLEncoder.encode(path,
					"UTF-8"));
			String type = map.getMimeTypeFromExtension(ext);
			return type;
		} catch (UnsupportedEncodingException e) {
			return null;
		}

	}

	public static void openFile(Context context, String path, String mimeType) {
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
		if (null == mimeType) {
			mimeType = "*/*";
		}
		intent.setDataAndType(Uri.fromFile(new File(path)), mimeType);
		context.startActivity(intent);
	}

	public static boolean deleteFile(Context context, String path) {
		File file = new File(path);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File file2 : files) {
				deleteFile(context, file2.getAbsolutePath());
			}
		}
		return file.delete();
	}

	public static void renameFile(Context context, String path) {
		final File file = new File(path);
		Builder builder = new Builder(context);
		AlertDialog dialog = builder.create();
		dialog.setTitle("Rename");
		dialog.setMessage("New name :");
		final EditText edtName = new EditText(context);
		edtName.setText(file.getName());
		dialog.setView(edtName);
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (!TextUtils.isEmpty(edtName.getText())) {
							File newFile = new File(file.getParentFile()
									.getAbsolutePath()
									+ File.separator
									+ edtName.getText().toString());
							file.setWritable(true);
							newFile.setWritable(true);
							file.renameTo(newFile);
							dialog.dismiss();
						}

					}
				});
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				});
		dialog.show();
	}

	public static void showDetail(Context context, String path) {
		File file = new File(path);
		Builder builder = new Builder(context);
		AlertDialog dialog = builder.create();
		dialog.setTitle("Details");
		String msg;
		msg = String.format("Name : \n%s", file.getName());
		msg += String.format("\nPath : \n%s", file.getAbsoluteFile());
		msg += String.format("\nSize : \n%s",
				AppUtils.convertSizeIntoString(file.length()));
		msg += String.format("\nLast Modified : \n%s", DateUtils.formatDateTime(
				context, file.lastModified(), DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_SHOW_YEAR));
		dialog.setMessage(msg);
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				});
		dialog.show();
	}

}
